package demo;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.logging.Level;
import java.util.logging.Logger;

import util.Configuration;
import util.po.ConfigPO;

/**
 * 第二个Socket服务端 用于接收客户端发过来的文件转发给服务端
 * 
 * @author sundy.hu
 * 
 * @version 1.0
 * 
 * @date 2012-11-28
 * 
 */
public class Server2 implements Runnable {

	// Socket服务器地址
	private String address;
	// Socket服务器地址
	private int port;
	// 本SocketServer端口
	private int server_port;

	private Logger log = Logger.getLogger(Server2.class.getName());

	private ConfigPO configInfo = new Configuration().configure();

	public Server2() {
		this.address = configInfo.getServerSocketAddress();
		this.port = configInfo.getServerSocketPort();
		this.server_port = configInfo.getServer2SocketPort();
		log.info("getter address " + address);
		log.info("getter port " + String.valueOf(port));
		log.info("getter server_port " + String.valueOf(server_port));
		this.log.setLevel(Level.INFO);
		this.configInfo = new Configuration().configure();
	}

	@Override
	public void run() {

		ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
				configInfo.getCorePoolSize(), configInfo.getMaximumPoolSize(),
				configInfo.getKeepAliveTime(), configInfo.getTimeUnit(),
				new ArrayBlockingQueue<Runnable>(
						configInfo.getBlockingqueueCapacity()));

		try {
			ServerSocket serverSocket = new ServerSocket(server_port);
			log.info("proxy server startup at port " + server_port);
			while (true) {
				Socket client = serverSocket.accept();
				Socket client2 = new Socket(address, port);
				threadPoolExecutor.execute(new Server2Receive(client, client2));
				// data = receiver(client);

				// send(client2, data);
				// System.out.println("AAAAAAAA");
				// Thread.sleep(10000);
			}

		} catch (IOException ex) {
			log.warning(ex.getLocalizedMessage());
			ex.printStackTrace();
		}
	}

	/*
	 * // 接收文件 private byte[] receiver(Socket client) { ByteArrayOutputStream
	 * bos = new ByteArrayOutputStream(); try { if (null != client) {
	 * BufferedInputStream input = new BufferedInputStream(
	 * client.getInputStream()); DataInputStream dis = new
	 * DataInputStream(input) ;
	 * 
	 * int len = 0; while ((len = dis.read()) != -1) { bos.write(len); }
	 * input.close(); client.close(); } } catch (IOException ex) {
	 * ex.printStackTrace(); } return bos.toByteArray(); }
	 * 
	 * // 发送文件给Socket服务端 private void send(Socket client, byte[] data) { try {
	 * if (null != client) { BufferedOutputStream output = new
	 * BufferedOutputStream( client.getOutputStream()); DataOutputStream dos =
	 * new DataOutputStream(output) ; dos.write(data); dos.flush(); dos.close();
	 * output.flush() ; output.close() ; client.close();
	 * System.out.println("files send successful"); } } catch (IOException ex) {
	 * System.out.println("client not connected server"); ex.printStackTrace();
	 * } }
	 */

}
